import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/format_utils.dart';
import 'package:nyh_motorcade_app/widgets/custom_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/widgets/custom_text_field.dart';
import 'package:nyh_motorcade_app/widgets/precision_limit_formatter.dart';
import 'package:nyh_motorcade_app/widgets/text_button_with_icon.dart';
import 'rapid_transfer_controller.dart';

class RapidTransferPage extends StatelessWidget {
  final controller = Get.put(RapidTransferController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.color_background,
      appBar: CustomAppBar(context: context, title: "快速划拨"),
      body: GetBuilder<RapidTransferController>(
        builder: (controller) {
          return Column(
            children: [
              Expanded(
                child: SingleChildScrollView(
                  physics: BouncingScrollPhysics(),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        color: Colours.color_white,
                        margin: EdgeInsets.only(top: 12.w),
                        padding: EdgeInsets.only(left: 16.w, right: 10.w),
                        height: 48.w,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              "拨出账户",
                              style: TextStyle(
                                color: Colours.color_text_theme,
                                fontSize: 14.sp,
                              ),
                            ),
                            TextButtonWithIcon(
                              onPressed: () {
                                controller.selectTransferOutAccount();
                              },
                              icon: Image.asset(
                                "images/ic_arrow_right_gray.png",
                                color: Colours.color_text_theme,
                              ),
                              label: Text(
                                controller.transferOutAccountName.isNotEmpty ? controller.transferOutAccountName : "请选择",
                                style: TextStyle(
                                  color: Colours.color_text_theme,
                                  fontSize: 14.sp,
                                ),
                              ),
                              textIconPadding: 8.w,
                              iconTextAlignment: IconTextAlignment.iconRightTextLeft,
                            ),
                          ],
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(vertical: 11.w, horizontal: 16.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              "拨出前余额 ¥${FormatUtils.formatMoney(controller.transferOutBeforeAmount)}",
                              style: TextStyle(
                                color: Colours.color_6f7082,
                                fontSize: 14.sp,
                              ),
                            ),
                            SizedBox(
                              width: 10.w,
                            ),
                            Expanded(
                              child: Text(
                                "拨出后余额 ¥${FormatUtils.formatMoney(controller.transferOutAfterAmount)}",
                                style: TextStyle(
                                  color: Colours.color_6f7082,
                                  fontSize: 14.sp,
                                ),
                                textAlign: TextAlign.right,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 16.w),
                        color: Colours.color_white,
                        height: 48.w,
                        child: Row(
                          children: [
                            Text(
                              "划拨金额",
                              style: TextStyle(
                                color: Colours.color_text_theme,
                                fontSize: 14.sp,
                              ),
                            ),
                            SizedBox(
                              width: 16.w,
                            ),
                            Expanded(
                              child: CustomTextField(
                                "请输入划拨金额",
                                controller: controller.amountController,
                                focusNode: controller.amountFocus,
                                hintStyle: TextStyle(color: Colours.color_cfcfd5, fontSize: 14.sp),
                                textStyle: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                                textAlign: TextAlign.right,
                                contentPadding: EdgeInsets.symmetric(vertical: 15),
                                keyboardType: TextInputType.numberWithOptions(decimal: true),
                                inputFormatters: [
                                  LengthLimitingTextInputFormatter(10),
                                  FilteringTextInputFormatter.allow(RegExp("[0-9.]")),
                                  PrecisionLimitFormatter(2),
                                ],
                                onChanged: (value) {
                                  controller.setTransferAmount(value);
                                },
                              ),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        color: Colours.color_white,
                        margin: EdgeInsets.only(top: 12.w),
                        padding: EdgeInsets.only(left: 16.w, right: 10.w),
                        height: 48.w,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              "拨入账户",
                              style: TextStyle(
                                color: Colours.color_text_theme,
                                fontSize: 14.sp,
                              ),
                            ),
                            TextButtonWithIcon(
                              onPressed: () {
                                controller.selectTransferInAccount();
                              },
                              icon: Image.asset(
                                "images/ic_arrow_right_gray.png",
                                color: Colours.color_text_theme,
                              ),
                              label: Text(
                                controller.transferInAccountName.isNotEmpty ? controller.transferInAccountName : "请选择",
                                style: TextStyle(
                                  color: Colours.color_text_theme,
                                  fontSize: 14.sp,
                                ),
                              ),
                              textIconPadding: 8.w,
                              iconTextAlignment: IconTextAlignment.iconRightTextLeft,
                            ),
                          ],
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(vertical: 11.w, horizontal: 16.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              "拨入前余额 ¥${FormatUtils.formatMoney(controller.transferInBeforeAmount)}",
                              style: TextStyle(
                                color: Colours.color_6f7082,
                                fontSize: 14.sp,
                              ),
                            ),
                            SizedBox(
                              width: 10.w,
                            ),
                            Expanded(
                              child: Text(
                                "拨入后余额 ¥${FormatUtils.formatMoney(controller.transferInAfterAmount)}",
                                style: TextStyle(
                                  color: Colours.color_6f7082,
                                  fontSize: 14.sp,
                                ),
                                textAlign: TextAlign.right,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
                        color: Colours.color_white,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              "备注",
                              style: TextStyle(
                                color: Colours.color_text_theme,
                                fontSize: 14.sp,
                              ),
                            ),
                            SizedBox(
                              height: 17.w,
                            ),
                            CustomTextField(
                              "请填写您的备注内容",
                              controller: controller.remarksController,
                              focusNode: controller.remarksFocus,
                              hintStyle: TextStyle(color: Colours.color_b7b7c0, fontSize: 14.sp),
                              textStyle: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp, height: 1.5),
                              fillColor: Colours.color_background,
                              borderRadius: BorderRadius.circular(10.w),
                              contentPadding: EdgeInsets.only(left: 16.w, top: 13.w, right: 10.w, bottom: 13.w),
                              inputFormatters: [LengthLimitingTextInputFormatter(100)],
                              onChanged: (value) {
                                controller.setRemarks(value);
                              },
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 16.w),
                color: Colours.color_white,
                child: TextButton(
                  onPressed: () {
                    controller.onConfirm();
                  },
                  child: Text(
                    "确定",
                    style: TextStyle(
                      color: Colours.color_white,
                      fontSize: 16.sp,
                    ),
                  ),
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.resolveWith((states) {
                      return controller.isComplete() ? Colours.color_theme : Colours.color_ffd599;
                    }),
                    overlayColor: MaterialStateProperty.resolveWith((states) {
                      return controller.isComplete() ? Colours.color_theme_deep : Colours.color_ffd599;
                    }),
                    shape: MaterialStateProperty.all(
                      RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(28.w),
                      ),
                    ),
                    minimumSize: MaterialStateProperty.all(Size(double.infinity, 48.w)),
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
